//
//  YTFindePasswordViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/8.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTFindePasswordViewModel.h"
#import "YTUserLoginAPI.h"

@interface YTFindePasswordViewModel ()

@property (copy, nonatomic, readwrite) NSString *passwordSure;

@property (strong, readwrite, nonatomic) RACSignal *passwordSureSignal;
@property (strong, readwrite, nonatomic) RACSignal *tipsHidden;
@property (strong, readwrite, nonatomic) RACCommand *resetCommand;

@end

@implementation YTFindePasswordViewModel

- (void)initialize {
    [super initialize];
    
    _passwordSureSignal = [[RACObserve(self, passwordSure) distinctUntilChanged] map:[NSObject yt_validateStrWithLenght:6]];
    
    @weakify(self);
    _tipsHidden = [[RACObserve(self, passwordSure) distinctUntilChanged] map:^id(id value) {
        @strongify(self);
        return @((BOOL)[self.password isEqualToString:self.passwordSure]);
    }];
    
    self.allValidate = [RACSignal combineLatest:@[self.phoneValidate, self.passwordValidate, self.verifyValidate, self.passwordSureSignal]
                                       reduce:^id(NSNumber *phone, NSNumber *password, NSNumber *verifyCode, NSNumber *passwordSure){
        return @(phone.boolValue && password.boolValue && verifyCode.boolValue && passwordSure.boolValue);
    }];
    
    self.resetCommand = [[RACCommand alloc] initWithEnabled:self.allValidate signalBlock:^RACSignal *(id input) {
        return [YTUserLoginAPI userFindPassWordWithPhone:self.phoneNum
                                                areaCode:[self.areaCode substringFromIndex:2]
                                              verfiyCode:self.verifyCode
                                                password:self.password];
    }];
}

@end
